//
//  LSZWelcomeViewController.swift
//  醒浪微博
//
//  Created by 雷顺哲 on 16/7/12.
//  Copyright © 2016年 LSZ. All rights reserved.
//

import UIKit

import SDWebImage

class LSZWelcomeViewController: UIViewController {
    // MARK:懒加载控件
//    背景图片
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
//     头像
    private lazy var headImageView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        
//        从沙盒中读取
        if let imageUrl = LSZUserAccountViewModel.sharedUserAccount.userAccount?.avatar_large{
            view.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        view.layer.cornerRadius = 42.5
        view.layer.masksToBounds = true
        return view
    }()
    
//    信息
    private lazy var messageLabel:  UILabel = {
        let label = UILabel()
        if let name = LSZUserAccountViewModel.sharedUserAccount.userAccount?.name {
            label.text = "欢迎回来, \(name)"
        } else {
            label.text = "欢迎回来"
        }
        
        label.alpha = 0
        label.font = UIFont.systemFontOfSize(12)
        label.textColor = UIColor.darkGrayColor()
        return label
    }()

    // MARK:自定义视图
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidDisappear(animated)
        startAnimation()
    }
//    添加控件的约束
    private func setUpUI() {
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(300)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.centerX.equalTo(headImageView)
        }
    }
//    需要在视图已经显示出来后再去执行动画
    func startAnimation() {
//        更新约束
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(100)
        }
        //usingSpringWithDamping 阻尼，大于0到1  阻尼越大 弹簧效果越小
        //initialSpringVelocity 初始速度
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in      
                
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            self.messageLabel.alpha = 1
            }, completion: { (_) -> Void in
                //  动画执行完成切换到主页
                
                //  切换跟视图的操作一般都放到appdelegate里面
                NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
                })
        }
        
    }

}
